package com.ruoyi.project.propertySystem.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.project.propertySystem.domain.ItemRecordPic;
import com.ruoyi.project.propertySystem.domain.vo.InspectItemRecordVO;
import com.ruoyi.project.propertySystem.mapper.ItemRecordPicMapper;
import com.ruoyi.project.query.InspectRecordQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.propertySystem.mapper.InspectItemRecordMapper;
import com.ruoyi.project.propertySystem.domain.InspectItemRecord;
import com.ruoyi.project.propertySystem.service.IInspectItemRecordService;

/**
 * 巡检项记录Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-18
 */
@Service
public class InspectItemRecordServiceImpl implements IInspectItemRecordService 
{
    @Autowired
    private InspectItemRecordMapper inspectItemRecordMapper;
    @Autowired
    private ItemRecordPicMapper itemRecordPicMapper;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询巡检项记录
     * 
     * @param id 巡检项记录ID
     * @return 巡检项记录
     */
    @Override
    public InspectItemRecord selectInspectItemRecordById(Long id)
    {
        return inspectItemRecordMapper.selectInspectItemRecordById(id);
    }

    /**
     * 查询巡检项记录列表
     * 
     * @param inspectItemRecord 巡检项记录
     * @return 巡检项记录
     */
    @Override
    public List<InspectItemRecord> selectInspectItemRecordList(InspectItemRecord inspectItemRecord)
    {
        return inspectItemRecordMapper.selectInspectItemRecordList(inspectItemRecord);
    }

    /**
     * 新增巡检项记录
     * 
     * @param inspectItemRecord 巡检项记录
     * @return 结果
     */
    @Override
    public int insertInspectItemRecord(InspectItemRecord inspectItemRecord)
    {
        return inspectItemRecordMapper.insertInspectItemRecord(inspectItemRecord);
    }

    /**
     * 修改巡检项记录
     * 
     * @param inspectItemRecord 巡检项记录
     * @return 结果
     */
    @Override
    public int updateInspectItemRecord(InspectItemRecord inspectItemRecord)
    {
        return inspectItemRecordMapper.updateInspectItemRecord(inspectItemRecord);
    }

    /**
     * 批量删除巡检项记录
     * 
     * @param ids 需要删除的巡检项记录ID
     * @return 结果
     */
    @Override
    public int deleteInspectItemRecordByIds(Long[] ids)
    {
        return inspectItemRecordMapper.deleteInspectItemRecordByIds(ids);
    }

    /**
     * 删除巡检项记录信息
     * 
     * @param id 巡检项记录ID
     * @return 结果
     */
    @Override
    public int deleteInspectItemRecordById(Long id)
    {
        return inspectItemRecordMapper.deleteInspectItemRecordById(id);
    }

    @Override
    public List<InspectItemRecordVO> getUserInspectRecordItemList(InspectRecordQuery inspectRecordQuery) {
        List<InspectItemRecordVO> userInspectRecordClassifyList = inspectItemRecordMapper.getUserInspectRecordClassifyList(inspectRecordQuery);
        userInspectRecordClassifyList.forEach(x->{
            //获取所有巡检项
            List<InspectItemRecordVO> userInspectRecordItemList = inspectItemRecordMapper.getUserInspectRecordItemByClassify(x.getClassifyId(), inspectRecordQuery.getInspectRecordId());
            userInspectRecordItemList.forEach(y -> {
                if(StringUtils.isNotBlank(y.getInspectView())) {
                    List<HashMap<String, Object>> strings = new ArrayList<>();
                    List<String> strings1 = new ArrayList<>();
                    String inspectView = y.getInspectView();
                    String[] split = inspectView.split("，");
                    for (String s : split) {
                        String[] split1 = s.split("\\|");
                        strings1.add(split1[0]);
                        strings1.add(split1[1]);
                    }
                    if (strings1.size() > 0) {
                        for (int i = 0; i < strings1.size(); i++) {
                            HashMap<String, Object> data = new HashMap<>();
                            data.put("value", i + "");
                            data.put("name", strings1.get(i));
                            strings.add(data);
                        }
                    }
                    y.setPickerData(strings);
                }
                //查询图片
                ItemRecordPic itemRecordPic = new ItemRecordPic();
                itemRecordPic.setItemRecordId(y.getInspectItemRecordId());
                List<ItemRecordPic> itemRecordPics = itemRecordPicMapper.selectItemRecordPicList(itemRecordPic);
                List<HashMap<String, String>> collect = itemRecordPics.stream().map(z -> {
                    HashMap<String, String> da = new HashMap<>();
                    da.put("url",serverConfig.getUrl()+z.getFilePath());
                    return da;
                }).collect(Collectors.toList());
//                List<String> collect = itemRecordPics.stream().map(z -> serverConfig.getUrl() + z.getFilePath()).collect(Collectors.toList());
//                y.setImgs(collect);
                y.setImageGround(collect);
            });
            x.setInspectItemRecordVOS(userInspectRecordItemList);
        });
        return userInspectRecordClassifyList;
    }
}
